package com.wutao.panzh.admin.content.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.panzh.UtilEntity.ResultVO;
import com.panzh.entity.InformationItem;
import com.panzh.myconst.CommonConst;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.admin.content.controller.imageUpload.DropzoneController;
import com.wutao.panzh.service.InforMationItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *
 * 此类主要写InformationItem（资讯类的相关Handler方法）
 */

@Controller
public class InformationItemController {

    @Reference
    private InforMationItemService inforMationItemService;
    
    @Autowired
    private DropzoneController dropzoneController;

    @RequestMapping("/get/Information/by/Id")
    @ResponseBody
    public ResultVO getInformatinById(@RequestParam("InformetionId") String InformetionId){
        InformationItem informationItemById = inforMationItemService.getInformationItemById(InformetionId);
        //未在数据库中查询到数据；
        if (informationItemById == null){
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_DOES_NOT_EXIST,ResultVO.NO_DATA);
        }else{
            //在数据库中查询到数据
            return new ResultVO<InformationItem>(ResultVO.SUCCESS,ResultVO.NO_MSG ,informationItemById);
        }
    }

    //删除InformationItem的方法；
    @RequestMapping("/delete/informationItme")
    @ResponseBody
    public ResultVO deleteInformationById(@RequestParam("informationId") String InformetionId){
        //如果抛出异常，进行捕获后返回
        try {
            inforMationItemService.deleteInformationById(InformetionId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DELETE_INFORMATION_EXCEPTION,ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.DELETE_INFORMATION_SUCCESS,ResultVO.NO_DATA);
    }

    //执行分页的方法：
    @ResponseBody
    @RequestMapping("/user/get/page")
    public ResultVO<PageInfo<InformationItem>> getUserPage(
            //由于pageNo在请求参数中不一定能够提供，所以设置默认值。
            //默认值为1表示未提供此数据时前往第1页
            @RequestParam(value="pageNo",defaultValue="1") Integer pageNo,
            //由于keyword在请求参数中不一定能够提供，所以设置默认值。
            //默认值为空字符串将不会对SQL语句有任何影响
            @RequestParam(value="keyword",defaultValue="") String keyword){

        PageInfo<InformationItem> informationItemPageInfo = inforMationItemService.getInformationItemPageInfo(pageNo, ProjectParames.PAGE_SIZE, keyword);

        return new ResultVO<PageInfo<InformationItem>>(ResultVO.SUCCESS,ResultVO.NO_MSG ,informationItemPageInfo);


    }

    //保存InformationItem的方法；
    @ResponseBody
    @RequestMapping("/save/informationItem")
    public ResultVO<String> saveInformationItem(InformationItem informationItem){
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复；
        String itemName = informationItem.getItemName();
        //调用方法获得同名的InformationItem；
        InformationItem resultInformaionItem = inforMationItemService.getInformatinByItemName(itemName);
        //如果为null，说明没有该信息，但是如果查到了，就证明系统已经有重名的资讯，直接拒绝插入；
        if (resultInformaionItem != null){
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS,ResultVO.NO_DATA);
        }
        //进行信息的插入；
        inforMationItemService.saveInformation(informationItem);
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY,ResultVO.NO_DATA);
    }

    //去往修改页面，必须携带数据；
    @RequestMapping("update_Information")
    public String toUpdateInformation(Model model, HttpServletRequest request){
        String itemId = request.getParameter("itemId");
        InformationItem informationItemById = inforMationItemService.getInformationItemById(itemId);
        //放入数据域
        model.addAttribute("informationItem",informationItemById);
        return "update_Information";
    }



    @ResponseBody
    @RequestMapping("/update/informationItem")
    public ResultVO<String> updateInformationItem(InformationItem informationItem){
        //这里应该是资讯名称不能相同，并不是ItemId，因为ItemId不可能重复，检查有没有同名的；
        String itemName = informationItem.getItemName();
        //调用方法获得同名的InformationItem,注意，本身可能存在数据库，可能会把本身查出来，所以必须要验证List的长度；
        List<InformationItem> resultInformations = inforMationItemService.getInformatinAllByItemName(itemName);
        //这里涉及一点小算法,只可能等于0或者等于1,如若size等于0，直接进行更新， 若size等于1，则比较id，若是id相同，则可以进行更新，若不同,拒绝更新
        if (resultInformations.size() == 0 ){
                inforMationItemService.updateInformation(informationItem);
                return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY,ResultVO.NO_DATA);
        }else{
            InformationItem resultInformation = resultInformations.get(0);
            if (resultInformation.getItemId().compareTo(informationItem.getItemId()) == 0){
                inforMationItemService.updateInformation(informationItem);
                return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_UPDATED_SUCCESSFULLY,ResultVO.NO_DATA);
            }
        }
        return new ResultVO<String>(ResultVO.FAILED, CommonConst.INFORMATION_ALREADY_EXISTS,ResultVO.NO_DATA);
    }

//    /upload/big/image/information;上传背景大图；这样子写真的好厉害喔，还是涛宝宝厉害，不，是layui厉害

    @ResponseBody
    @RequestMapping("/upload/big/image/information")
    public ResultVO<String> uploadBigImageInformation(@RequestParam("file") MultipartFile image, @RequestParam("id") String informationItemId){

        try {
            Map<String, Object> imageMap = dropzoneController.upload(image);
            String imagSrc = (String) imageMap.get("fileName");
            inforMationItemService.insertInformationImageSrc(imagSrc,informationItemId);
        } catch (Exception e) {
            return new ResultVO<String>(ResultVO.FAILED, CommonConst.DATA_SAVING_FAILED,ResultVO.NO_DATA);
        }
        return new ResultVO<String>(ResultVO.SUCCESS, CommonConst.INFORMATION_SAVED_SUCCESSFULLY,ResultVO.NO_DATA);
    }
}